﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class WorkClassDA : IWorkClassDA
    {
        private string CREATE_DATE = "@CREATE_DATE";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_WORK_CLASS_ID_UNIQUE = " SELECT COUNT(1) FROM @_@WORK_CLASS WHERE WORK_CLASS_ID = @WORK_CLASS_ID ";
        private string SQL_DELETE_WORK_CLASS = " DELETE FROM @_@WORK_CLASS WHERE WORK_CLASS_ID = @WORK_CLASS_ID ";
        private string SQL_INSERT_WORK_CLASS = " INSERT INTO @_@WORK_CLASS ( WH_ID, WORK_CLASS_ID, DESCR, REMARK, IS_ACTIVE, CREATED_BY, CREATE_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @WORK_CLASS_ID, @DESCR, @REMARK, @IS_ACTIVE, @CREATED_BY, @CREATE_DATE, @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_WORK_CLASS = " SELECT  ROWID, WH_ID, WORK_CLASS_ID, DESCR, REMARK, IS_ACTIVE, TS, CREATED_BY, CREATE_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@WORK_CLASS ";
        private string SQL_UPDATE_WORK_CLASS = " UPDATE @_@WORK_CLASS SET WH_ID = @WH_ID, DESCR = @DESCR, REMARK = @REMARK, IS_ACTIVE = @IS_ACTIVE, CREATE_DATE = @CREATE_DATE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE WORK_CLASS_ID = @WORK_CLASS_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";
        private string WORK_CLASS_ID = "@WORK_CLASS_ID";

        public bool CheckWorkClassIDUnique(string workClassID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WORK_CLASS_ID", workClassID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_WORK_CLASS_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckWorkClassIDUnique(DataBase dataBase, DbTransaction tran, string workClassID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WORK_CLASS_ID", workClassID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_WORK_CLASS_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteWorkClass(List<string> workClassID, List<string> whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < workClassID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WORK_CLASS_ID", workClassID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_WORK_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int DeleteWorkClass(DataBase dataBase, DbTransaction tran, List<string> workClassID, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    for (int j = 0; j < workClassID.Count; j++)
                    {
                        SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WORK_CLASS_ID", workClassID[j]) };
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_WORK_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllWorkClass(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_WORK_CLASS.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllWorkClass(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_WORK_CLASS.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public WorkClassInfo GetWorkClassByID(string workclassID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_WORK_CLASS.Replace("@_@", whLoginID) + " WHERE WORK_CLASS_ID = @WORK_CLASS_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WORK_CLASS_ID", workclassID) };
            WorkClassInfo workClassInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    workClassInfo = new WorkClassInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["WORK_CLASS_ID"].ToString(), reader["DESCR"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATE_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATE_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return workClassInfo;
        }

        public WorkClassInfo GetWorkClassByID(DataBase dataBase, DbTransaction tran, string workclassID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_WORK_CLASS.Replace("@_@", whLoginID) + " WHERE WORK_CLASS_ID = @WORK_CLASS_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@WORK_CLASS_ID", workclassID) };
            WorkClassInfo workClassInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                workClassInfo = new WorkClassInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["WORK_CLASS_ID"].ToString(), reader["DESCR"].ToString(), reader["REMARK"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATE_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATE_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return workClassInfo;
        }

        public DataSet GetWorkClassByQueryList(List<string> sqlWhere, WorkClassQueryEntity workclassQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_WORK_CLASS.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!workclassQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, workclassQuery.CurrentPage, workclassQuery.PageSize, workclassQuery.SortField, workclassQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetWorkClassByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, WorkClassQueryEntity workclassQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_WORK_CLASS.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, workclassQuery.CurrentPage, workclassQuery.PageSize, workclassQuery.SortField, workclassQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertWorkClass(WorkClassInfo workClassInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    workClassInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_WorkClass_Parameters(workClassInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_WORK_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertWorkClass(DataBase dataBase, DbTransaction tran, WorkClassInfo workClassInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    workClassInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_WorkClass_Parameters(workClassInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_WORK_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_WorkClass_Parameters(WorkClassInfo workClassInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.WORK_CLASS_ID, SqlDbType.VarChar, 100), new SqlParameter(this.DESCR, SqlDbType.VarChar, 300), new SqlParameter(this.REMARK, SqlDbType.VarChar, 300), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.CREATE_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30) };
            if (!string.IsNullOrEmpty(workClassInfo.WhID))
            {
                paramArray[0].Value = workClassInfo.WhID;
            }
            else
            {
                paramArray[0].Value = DBNull.Value;
            }
            paramArray[1].Value = workClassInfo.WorkClassID;
            paramArray[2].Value = workClassInfo.Descr;
            if (!string.IsNullOrEmpty(workClassInfo.Remark))
            {
                paramArray[3].Value = workClassInfo.Remark;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            paramArray[4].Value = workClassInfo.IsActive;
            paramArray[5].Value = workClassInfo.CreatedBy;
            paramArray[6].Value = workClassInfo.CreateDate;
            paramArray[7].Value = workClassInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateWorkClass(WorkClassInfo workClassInfo, List<string> whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    workClassInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_WorkClass_Parameters(workClassInfo);
                    if (paras != null)
                    {
                        result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_WORK_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int UpdateWorkClass(DataBase dataBase, DbTransaction tran, WorkClassInfo workClassInfo, List<string> whLoginID)
        {
            int result = 0;
            try
            {
                for (int i = 0; i < whLoginID.Count; i++)
                {
                    workClassInfo.WhID = whLoginID[i].Substring(0, whLoginID[i].Length - 1);
                    SqlParameter[] paras = this.Set_WorkClass_Parameters(workClassInfo);
                    if (paras != null)
                    {
                        result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_WORK_CLASS.Replace("@_@", whLoginID[i]), paras);
                    }
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

